package bean;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(name = "ChangeImg")
@MultipartConfig(location = "C:\\Users\\asus\\Desktop\\student-login\\src\\main\\webapp\\img")// location改为本地的绝对路径
public class img_change extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        Part part = request.getPart("img");

        String fileName = part.getSubmittedFileName();

        part.write(fileName);

        String str="img/"+fileName;
        User user=(User)request.getSession().getAttribute("LocalUser");
        user.setImg(str);
        request.getSession().setAttribute("LocalUser",user);   //将session里面的用户更换头像后存入

        function.UploadImg(user);    //新的用户信息写入文件

        response.sendRedirect("/interface.jsp");

    }


}
